﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace VBACodeingTool
{
    public partial class Popup : Form
    {
        public Popup()
        {
            InitializeComponent();
        }

        public int X { get; set; }
        public int Y { get; set; }
        public bool IsShow { get; private set; } = false;
        private List<ProjectCodeData> ShowContents { get; set; }
        public List<ProjectCodeData> AllContents { private get; set; }
        private List<ProjectCodeData> SubContents { get; set; }
        private List<ProjectCodeData> VariableContents { get; set; }

        private void Popup_Load(object sender, EventArgs e)
        {
            ShowContents = AllContents;
            ChangedItem();
            if (listBox1.Items.Count > 0)
            {
                this.listBox1.SelectedIndex = 0;
            }
            this.Location = new Point(X, Y);
            IsShow = true;
        }

        public void CloseWindow()
        {
            Debug.WriteLine("窗口关闭了");
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(() =>
                {
                    this.Close();
                    IsShow = false;
                }));
            }
            else
            {
                this.Close();
                IsShow = false;
            }
        }

        private void ChangedItem()
        {
            this.listBox1.Items.Clear();
            for (int i = 0; i < ShowContents.Count; i++)
            {
                this.listBox1.Items.Add(ShowContents[i].ShortCut);
            }
        }

        private void ListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string content = this.listBox1.SelectedItem.ToString();
            VBEHelper.GetCurrentCodePane().Window.SetFocus();
            VBEHelper.InsertCurrentLineStr(content,ProgramInstanceManager.ShortCut);
            CloseWindow();
        }

        public string GetSelectedContent()
        {
            return this.listBox1.SelectedItem.ToString();
        }

        private async Task<List<ProjectCodeData>> FilterCodeData(ProjectCodeType projectCodeType)
        {
            return await Task.Run(() =>
            {
                List<ProjectCodeData> list = new List<ProjectCodeData>();
                foreach (ProjectCodeData item in AllContents)
                {
                    if (item.CodeType == projectCodeType)
                    {
                        list.Add(item);
                    }
                }
                return list;
            });
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Tab) 
            {
                string content = this.listBox1.SelectedItem.ToString();
                VBEHelper.GetCurrentCodePane().Window.SetFocus();
                VBEHelper.InsertCurrentLineStr(content, ProgramInstanceManager.ShortCut);
                CloseWindow();
                return true; 
            }
            return base.ProcessCmdKey(ref msg, keyData); 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ShowContents = AllContents;
            ChangedItem();
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            VariableContents = await FilterCodeData(ProjectCodeType.Variable);
            ShowContents = VariableContents;
            ChangedItem();
        }

        private async void button3_Click(object sender, EventArgs e)
        {
            SubContents = await FilterCodeData(ProjectCodeType.Sub);
            ShowContents = SubContents;
            ChangedItem();
        }
    }
}